<template>
	<view class="flex-col page">
		<view>
			<view :style="{height:statusBarHeight+'px'}"></view>
			<u-navbar title="商品搜索" bgColor="#F5F5F5" @leftClick="$nav(2)">	
			</u-navbar>
		</view>
		
		<view class="search-box">
			<mSearch class="mSearch-input-box" :mode="2" :placeholder="defaultKeyword" @search="doSearch(false)" @confirm="doSearch(false)" v-model="keyword"></mSearch>
		
		</view>
		<view class="search-keyword">
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block" v-if="oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view>历史搜索</view>
						<view>
							<image @tap="oldDelete" src="/static/delete.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{keyword}}</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import mSearch from '@/components/search.vue'
	export default {
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				isShowKeywordList: false,
				oldKeywordList: [],
				
			};
		},
		components: {
			mSearch
		},
		computed:{
			statusBarHeight(){
				return this.$store.state.statusBarHeight + 44
			}
		},
		onLoad() {
			this.loadOldKeyword();	
		},
		methods:{
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "默认关键字";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//执行搜索
			doSearch(keyword) {
				keyword = keyword===false?this.keyword:keyword;
				this.keyword = keyword;
				this.saveKeyword(keyword); //保存为历史 
				// uni.showToast({
				// 	title: keyword,
				// 	icon: 'none',
				// 	duration: 2000
				// });
				uni.navigateTo({
					url: '/pages/search/search_child?content=' + keyword
				})
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.page{
	width: 100%;
	overflow-y: auto;
	overflow-x: hidden;
	height: 100%;
	background-color: #FFF;
}
.search-box {
	width:100%;
	background-color:rgb(242,242,242);
	padding:15upx 2.5%;
	display:flex;
	justify-content:space-between;
	position:sticky;
	top: 0;
}
.search-box .mSearch-input-box{
	width: 100%;
}
.search-keyword {
	width:100%;
	background-color:rgb(242,242,242);
}
.keyword-box {
	height:calc(100vh - 110upx);
	border-radius:20upx 20upx 0 0;
	background-color:#fff;
}
.keyword-box .keyword-block {
	padding:10upx 0;
}
.keyword-box .keyword-block .keyword-list-header {
	width:94%;
	padding:10upx 3%;
	font-size:27upx;
	color:#333;
	display:flex;
	justify-content:space-between;
}
.keyword-box .keyword-block .keyword-list-header image {
	width:40upx;
	height:40upx;
}
.keyword-box .keyword-block .keyword {
	width:94%;
	padding:3px 3%;
	display:flex;
	flex-flow:wrap;
	justify-content:flex-start;
}
.keyword-box .keyword-block .hide-hot-tis {
	display:flex;
	justify-content:center;
	font-size:28upx;
	color:#6b6b6b;
}
.keyword-box .keyword-block .keyword>view {
	display:flex;
	justify-content:center;
	align-items:center;
	border-radius:60upx;
	padding:0 20upx;
	margin:10upx 20upx 10upx 0;
	height:60upx;
	font-size:28upx;
	background-color:rgb(242,242,242);
	color:#6b6b6b;
}

</style>
